package com.myapp.ruyixing.ui.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.utils.LoginHelper;

/**
 * 拨打电话
 * 
 * @author laiwenjie
 * @date 2012-12-20
 */
public class PickPhoneCallFragment extends DialogFragment {

	private static final String PHONE_NUMBERS = "phone_numbers";

	public static PickPhoneCallFragment newInstance(String[] phoneNumbers,
			String shopid) {
		PickPhoneCallFragment frag = new PickPhoneCallFragment();
		Bundle args = new Bundle();
		args.putStringArray(PHONE_NUMBERS, phoneNumbers);
		args.putString("shopid", shopid);
		frag.setArguments(args);
		return frag;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final String[] phoneNumbers = getArguments().getStringArray(
				PHONE_NUMBERS);
		final String mPhoneNumber = phoneNumbers[0];

		if (phoneNumbers.length == 1) {
			return new AlertDialog.Builder(getActivity())
					.setIcon(R.drawable.device_access_call)
					.setTitle(
							String.format(
									getString(R.string.comfirm_phone_call_title_with_number),
									phoneNumbers[0]))
					.setPositiveButton(R.string.alert_dialog_ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									confirmCallPhone(phoneNumbers[0]);
								}
							})
					.setNegativeButton(R.string.alert_dialog_cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();

		} else {
			return new AlertDialog.Builder(getActivity())
					.setIcon(R.drawable.device_access_call).setTitle("拨打电话")
					.setItems(phoneNumbers, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							confirmCallPhone(phoneNumbers[which]);

						}
					}).create();
		}
	}

	private void confirmCallPhone(String phoneNumber) {
		if (!makeCall(getActivity(), phoneNumber)) {
			Toast.makeText(getActivity(),
					getString(R.string.err_tip_tel_net_exists),
					Toast.LENGTH_LONG).show();
		} else {
			Context context = getActivity();
			String bduss = LoginHelper.getInstance().getBDUSS();
			String shopid = getArguments().getString("shopid");
			RYHttpClient.recordCall(context, new RYHttpResponseHandler() {

				@Override
				public void onSuccess(Object o) {
				}

				@Override
				public void onFailure(Throwable throwable) {
				}
			}, bduss, shopid, phoneNumber);
		}
	}

	private boolean makeCall(Context context, String phoneNumber) {
		TelephonyManager telMgr = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String url = "tel:" + phoneNumber;
		if (telMgr != null
				&& telMgr.getSimState() == TelephonyManager.SIM_STATE_READY) {
			context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(url)));

			return true;
		}

		return false;
	}
}